package net.wesley.android.city;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import net.wesley.android.City;

public class Yinchuan extends City {

	public void requestParamsFinish(String[] values) {
		final String chejia4=values[0];
		final DefaultHttpClient httpClient = new DefaultHttpClient();  
		try {
			{
				HttpPost post=new HttpPost("http://www.nxjj.gov.cn/sys/jdcwzGuest_init.act");
				post.setHeader("Referer", "http://www.nx.cninfo.net/zhuanti/wz/index.html");
				post.setEntity(new StringEntity(String.format("hpzl=02&hphm=%s&clsbdh=%s&fdjh=%s&Submit=+%%E9%%87%%8D+%%E5%%A1%%AB+&=false",
						URLEncoder.encode(chepai), URLEncoder.encode(values[0]),URLEncoder.encode(values[1]))));
				post.setHeader("Content-Type","application/x-www-form-urlencoded");
				HttpResponse rsp = httpClient.execute(post);	
				String line, result = "";
				BufferedReader br = new BufferedReader(new InputStreamReader(rsp.getEntity()
						.getContent(),"UTF-8"));
				while ((line = br.readLine()) != null) result += line+"\n";
				String [] rets=result.split("\\\\\"");
				if (rets[7].equals("NO")){
					callback.onComplete(-10086, "ret:查询失败："+rets[3]);
					return;
				}
			}
			HttpPost post=new HttpPost("http://www.nxjj.gov.cn/sys/jdcwzGuest_displayAll.act");
			post.setHeader("Referer", "http://www.nx.cninfo.net/zhuanti/wz/index.html");
			post.setEntity(new StringEntity(String.format("hpzl=02&hphm=%s&clsbdh=%s&fdjh=%s",
					URLEncoder.encode(chepai), URLEncoder.encode(values[0]),URLEncoder.encode(values[1]))));
			post.setHeader("Content-Type","application/x-www-form-urlencoded");
			HttpResponse rsp = httpClient.execute(post);	
			String line, result = "";
			BufferedReader br = new BufferedReader(new InputStreamReader(rsp.getEntity()
					.getContent(),"UTF-8"));
			while ((line = br.readLine()) != null) result += line+"\n";
			processResult(result);				
		} catch (ClientProtocolException e) {
			callback.onComplete(-10001, "");
		} catch (IOException e) {
			callback.onComplete(-10002, "");
		}
	}

	private void processResult(String result) {
		int idx=result.indexOf("缴款状态</b>");
		if (idx>0){
			String str=result.substring(idx).split("</tbody>")[0];
			String[] rows=str.split("<tr>");
			String retvalue="";
			for (int i=1;i<rows.length;i++){
				String[] cols=rows[i].split("<td");
				String shijian=URLDecoder.decode(cols[3].split("</td")[0].split(">")[1].trim());
				String didian=URLDecoder.decode(cols[2].split("</td")[0].split(">")[1].trim());
				String code=URLDecoder.decode(cols[1].split("</td")[0].split(">")[1].trim());
				retvalue+=String.format("shijian:%s\ndidian:%s\ncode:%s\n\n",shijian,didian,code);
			}
			callback.onComplete(rows.length-1, retvalue);
			return;
		}
		callback.onComplete(0, "");
	}	

	@Override
	public boolean match(String chepai) {
		return chepai.startsWith("宁A"); 
		}

	@Override
	public String[] paramDesc() {
		String[] s={"车辆识别代号","发动机号"};
		return s;
	}

}
